package com.ym.manager.sys.config;

import com.ym.manager.sys.config.model.SysConfigInfo;
import com.ym.manager.sys.model.AdminUser;
import com.ym.utils.Pages;
import com.ym.utils.ResultUtil;
import com.ym.web.bind.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: ConfigController
 * @Date: 2023/2/16 11:04
 * @Description:
 * @Version: 1.0
 */
@RequestMapping("/sys/config")
@Controller
public class ConfigController {
    @Autowired
    private SystemConfigService systemConfigService;

    @RequestMapping("/sys/index")
    public String index(Model model){
        return "config/sysList";
    }

    @ResponseBody
    @RequestMapping("/sys/list")
    public String sysList(@RequestParam(defaultValue = "0") int start,
                       @RequestParam(defaultValue = "10")int length,
                       @RequestParam(defaultValue = "")String configKey,
                       @RequestParam(defaultValue = "")String remark,
                       @CurrentUser AdminUser user){
        Map<String,Object> result = ResultUtil.result();
        try {
            Pages<SysConfigInfo> pages = systemConfigService.getList(start,length,configKey,remark,1);
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
            result.put("recordsTotal",pages.getTotal());
            result.put("recordsFiltered",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    @RequestMapping("/walletindex")
    public String walletIndex(Model model){
        return "config/walletList";
    }

    @ResponseBody
    @RequestMapping("/wallet/list")
    public String walletList(@RequestParam(defaultValue = "0") int start,
                          @RequestParam(defaultValue = "10")int length,
                          @RequestParam(defaultValue = "")String configKey,
                          @RequestParam(defaultValue = "")String remark,
                          @CurrentUser AdminUser user){
        Map<String,Object> result = ResultUtil.result();
        try {
            Pages<SysConfigInfo> pages = systemConfigService.getList(start,length,configKey,remark,2);
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
            result.put("recordsTotal",pages.getTotal());
            result.put("recordsFiltered",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }
    @ResponseBody
    @RequestMapping("/update")
    public String update(
            @RequestParam(defaultValue = "")String configKey,
            @RequestParam(defaultValue = "")String configValue,
            @RequestParam(defaultValue = "0")int type){
        Map<String,Object> result = ResultUtil.result();
        systemConfigService.update(configKey,configValue,type);
        return ResultUtil.toJSON(result);

    }
    @ResponseBody
    @RequestMapping("/del")
    public String del(
            @RequestParam(defaultValue = "")String configKey,
            @RequestParam(defaultValue = "0")int type){
        Map<String,Object> result = ResultUtil.result();
        systemConfigService.del(configKey,type);
        return ResultUtil.toJSON(result);

    }
}
